<body>
    <link rel="stylesheet" href="static/css/AuthenticationCodeManagement.css">
    <link rel="stylesheet" href="static/css/base.css">
    <div class="AuthenticationCodeManagement" id="AuthenticationCodeManagement">
        <div class="centent">
            <table cellspacing="0" cellpadding="0">
                <thead class="centent_top">
                    <tr>
                        <th class="centent_top_1 centent_top_">部门</th>
                        <th class="centent_top_2 centent_top_">认证码</th>
                        <th class="centent_top_3 centent_top_">操作</th>
                    </tr>

                </thead>
                <thead>

                </thead>






                <tbody class="centent_bottom" style="width: 100%;">
                    <template v-for="(p,index) of passwordInfo">
                        <tr>
                            <td class=" department centent_bottom_"> {{p.DepartmentName}}</td>
                            <td class="authenticationCode centent_bottom_">

                                <div class="authenticationCode_a"> {{p.password}}</div>
                                <div class="authenticationCode_b"></div>
                            </td>
                            <td class="operation   centent_bottom_ ">
                                <div @click="open($event,p)" class="operation_">修改</div>
                            </td>


                        </tr>
                    </template>


                </tbody>
            </table>
        </div>
    </div>


    <script>
        console.log("authentiv");
        const c = window.cloud
        Vue.prototype.cloud = c
        Vue.config.productionTip = false //阻止vue在启动时生成生产提示
        Vue.config.devtools = true;
        const vm = new Vue({
            el: '.AuthenticationCodeManagement',
            data() {

                return {

                    //inputMessage输入信息
                    passwordInfo: [], //用户对象.
                    password: '',//输入的密码
                    confirmPassword: '',//确认密码
                    openid: "",//保存的临时
                    realName: "",
                    department: "网络研发部",
                    Postion: '',
                    istrue: true,
                    isReset: true,
                    DepartmentName: '',
                }
            },
            methods: {


                //配置微信
                async initWx() {
                    //创建新的cloud实例
                    var c1 = new this.cloud.Cloud({
                        identityless: true,
                        // 资源方 AppID 填自己的
                        resourceAppid: 'wx96f760966921f98a',
                        // 资源方环境 ID	填自己的
                        resourceEnv: 'cloud1-8g54d6xb74e96e7d'
                    })
                    //初始化,使用await将异步转化为同步
                    await c1.init()
                    console.log('初始化完成');
                    this.cloud = c1
                    this.db = this.cloud.database()
                },
                async getMessage() {
                    this.db.collection("Password").get().then(res => {
                        console.log(res);
                        var dataArray = res.data
                        var arraryLength = res.data.length
                        this.passwordInfo.splice(0, this.passwordInfo.length);
                        for (let i = 0; i < arraryLength; i++) {
                            this.passwordInfo.push(dataArray[i])
                        }
                        console.log(this.passwordInfo);
                    }

                    )
                },
                open(e, p) {
                    console.log(p);
                    this.$prompt('请输入新的认证码', '修改认证码', {
                        showCancelButton: true,
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',

                        beforeClose: (action, instance, done) => {
                            console.log(instance);
                            if (action === 'confirm') {
                                instance.confirmButtonLoading = true;
                                instance.confirmButtonText = '执行中...';
                                this.db.collection('Password').where({ DepartmentName: p.DepartmentName }).update({

                                    data: {

                                        "password": instance.inputValue
                                    },
                                    success: res => {
                                        console.log(res);
                                        if (res.stats.updated) {
                                            setTimeout(() => {
                                                done();
                                                setTimeout(() => {
                                                    instance.confirmButtonLoading = false;
                                                }, 300);
                                            }, 1000);
                                        }
                                    },
                                    fail: res => {

                                        console.log(res);

                                    }
                                })

                            } else {
                                done();
                            }
                        }
                    }).then(({ value }) => {
                        this.$message({
                            type: 'success',
                            message: '你的认证码是: ' + value
                        });
                        this.db.collection("Password").get().then(res => {
                            console.log(res);
                            // 我要的数据是res.data
                            this.passwordInfo = res.data;
                        }
                        )
                    }).catch((error) => {
                        console.log(error);
                    });

                },




            },
            async created() {

                await this.initWx();
                //获取列表数据
                this.getMessage();
                console.log(this);
            }








        })

    </script>
</body>